package com.st.service.impl;

import com.alibaba.fastjson.JSON;
import com.api.scenic.IScenicClient;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.st.entity.Result;
import com.st.entity.ScenicDetail;
import com.st.entity.Ticket;
import com.st.mapper.ITicketMapper;
import com.st.service.ITicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TicketServiceImpl extends ServiceImpl<ITicketMapper, Ticket> implements ITicketService {
    @Autowired
    private ITicketMapper iTicketMapper;
    @Autowired
    private IScenicClient iScenicClient;


    @Override
    public Ticket getTicketByAid(Integer aid) {
        ScenicDetail scenicDetailByAid = iScenicClient.getScenicDetailByAid(aid);
        return iTicketMapper.selectById(scenicDetailByAid.getAttractionsId());
    }

    @Override
    public List findAll() {
        QueryWrapper queryWrapper = new QueryWrapper();
        return iTicketMapper.selectList(queryWrapper);
    }

    @Override
    public Ticket getTicketBy(Integer aid) {
        return iTicketMapper.selectById(aid);
    }

    //扣库存
    public Result delNum(String delNum){
        Map parse = (Map) JSON.parse(delNum);
        Integer id = (Integer) parse.get("id");
        Integer num = (Integer) parse.get("num");

        Ticket ticket = iTicketMapper.selectById(id);

        ticket.setNum(ticket.getNum()-num);
        int i = iTicketMapper.updateById(ticket);
        return new Result(i==1?true:false,i==1?"扣除成功":"扣除失败");
    }

}
